package fxLib
{
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	
	import flash.display.MovieClip;
	import flash.utils.getTimer;
	
	public class FXRotate
	{
		// set defaults for gettersetters
		private var mnRotationsPerSecond:Number = 0.0;
		private var mnRotationDirection:int     = 1;
		private var mnRotationDuration:Number   = 0;
		private var mnInitialAngle:Number       = 0;
		
		private var mnMCInitialRotation:Number;
		
		private var mnAnglets:Number;
		private var mnRotationStartTime:int;
		
		private var mcRotateTween:Tween;
		
		private var mbRotateDone:Boolean        = false;
		
		private var mcRotateObject:MovieClip;
		
		// constructor
		public function FXRotate( acMCtoRotate:MovieClip, acParams:Object = null )
		{
			// accept parameters if given, in Object format
			if (acParams != null)
			{
				if ( !isNaN( acParams.rotationsPerSecond ) )
					mnRotationsPerSecond        = acParams.rotationsPerSecond;
				
				if ( !isNaN( acParams.rps ) )
					mnRotationsPerSecond        = acParams.rps;
				
				if ( !isNaN( acParams.rotateDuration ) )
					mnRotationDuration          = acParams.rotateDuration;
				
				if ( !isNaN( acParams.duration ) )
					mnRotationDuration          = acParams.duration;
				
				if ( !isNaN( acParams.direction ) )
				{
					if (acParams.direction == -1 || acParams.direction == 1)
						mnRotationDirection     = acParams.direction;
				}
			}
			
			mcRotateObject                      = acMCtoRotate;
			
			mnMCInitialRotation                 = mcRotateObject.rotation;
			
			var bOKTORotate:Boolean      = true;
			
			// defensive code
			if ( mnRotationsPerSecond == 0 )
			{
				bOKTORotate              = false; // we cannot go forward
			}
			
			if (mnRotationDuration == 0)
			{
				mnRotationDuration              = Number.POSITIVE_INFINITY;
			}
			
			// hm, maybe this is not so good an idea, using Tweens for this.
			
			if (bOKTORotate)
			{
				mbRotateDone             = false;
				
				mnAnglets                = 360 * mnRotationsPerSecond / 1000;
				mnRotationStartTime      = getTimer();
				
				mcRotateTween            = new Tween( this, "updateRotation", None.easeNone, mnMCInitialRotation, mnMCInitialRotation, mnRotationDuration, true );
				
				mcRotateTween.addEventListener( TweenEvent.MOTION_FINISH, rotateFinished, false, 0, true );
			}
			else
			{
				destroy();
			}
		}
		
		private function rotateFinished( event:TweenEvent ):void
		{
			mcRotateTween.removeEventListener( TweenEvent.MOTION_FINISH, rotateFinished, false );
			
			destroy();
		}
		
		public function set updateRotation( newValue:Number ):void
		{
			// ignore argument, won't work
			var nTimeInto:int            = getTimer() - mnRotationStartTime;
			var nCurrentAnglets:Number   = nTimeInto * mnAnglets;
			
			mcRotateObject.rotation      = (nCurrentAnglets % 360.00);
		}
		
		public function destroy():void
		{
			mcRotateTween = null;
			mcRotateObject = null;
		}
		
		public function stop():void
		{
			mcRotateTween.removeEventListener( TweenEvent.MOTION_FINISH, rotateFinished, false );
			
			destroy();
		}
	}
}